#!/usr/bin/Rscript


library('getopt')
library('debar')


# here 2 means must give input
spec <- matrix(
  c("input",  "i", 2, "character", "input file path",
    "cpu", "c", 1, "integer",  "cpu number",
    "out",  "o", 2, "character",  "output file path",
    "help",   "h", 0, "logical",  "Help"),
  byrow=TRUE, ncol=5)

opt <- getopt(spec=spec)


if( !is.null(opt$help) || is.null(opt$input) || is.null(opt$out) ){
  cat(paste(getopt(spec=spec, usage = T), "\n"))
  quit()
}

if (is.null(opt$cpu)){
  multicor = 1
}else{
  multicor = opt$cpu
}


sprintf('work with %s cpu', multicor)


inpath = opt$input
outpath = opt$out


if (!dir.exists(outpath)){
  dir.create(outpath)
}else{
  unlink(outpath, recursive = TRUE, force = TRUE)
}


# give the input path

for (i in list.files(inpath)){
  #print('')
  p = paste(inpath,'/',i,sep = '')
  o = paste(outpath,'/',i,sep='')
  denoise_file(p, outfile =o,multicore=multicor,keep_rejects = TRUE)
}



